﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AssemblyCYG
{
    public partial class MVSCamera : Form
    {
        System.Diagnostics.Stopwatch takePicTime = new System.Diagnostics.Stopwatch();     //取图时间
        public MVSCamera()
        {
            InitializeComponent();
            CloseExistCameral();                                              //首先关闭已有相机
            InitialControl();                                                 //初始化控件
            ShareData.MvsCameral.captureImage += MvsCameral_captureImage;     //委托采集图像
            InitialCameralDeveice();          //初始化相机设备 
            LoadCameraPara(); 
        }
        private void LoadCameraPara()
        {
            this.tB_glueCamName.Text = ProjectInfo.Parameter.glueCamName;
            this.tB_guideCamName.Text = ProjectInfo.Parameter.guideCamName;
            this.tB_downCamName.Text = ProjectInfo.Parameter.downCamName;
            this.tB_upCamName.Text = ProjectInfo.Parameter.upCamName;
            this.tB_reviewCamName.Text = ProjectInfo.Parameter.reviewCamName;

            this.tB_glueCam_expose.Text = ProjectInfo.Parameter.glueCamExposure.ToString();
            this.tB_guideCam_expose.Text = ProjectInfo.Parameter.guideCamExposure.ToString();
            this.tB_downCam_expose.Text = ProjectInfo.Parameter.downCamExposure.ToString();
            this.tB_upCam_exposeTime1.Text = ProjectInfo.Parameter.upCamExposure1.ToString();
            this.tB_upCam_exposeTime2.Text = ProjectInfo.Parameter.upCamExposure2.ToString();
            this.tB_reviewCam_expose1.Text = ProjectInfo.Parameter.reviewCamExposure1.ToString();
            this.tB_reviewCam_expose2.Text = ProjectInfo.Parameter.reviewCamExposure2.ToString();
            this.tB_reviewCam_expose3.Text = ProjectInfo.Parameter.reviewCamExposure3.ToString();

            this.tB_glueCam_gain.Text = ProjectInfo.Parameter.glueCamGain.ToString();
            this.tB_guideCam_gain.Text = ProjectInfo.Parameter.guideCamGain.ToString();
            this.tB_downCam_gain.Text = ProjectInfo.Parameter.downCamGain.ToString();
            this.tB_upCam_gain1.Text = ProjectInfo.Parameter.upCamGain1.ToString();
            this.tB_upCam_gain2.Text = ProjectInfo.Parameter.upCamGain2.ToString();
            this.tB_reviewCam_gain1.Text = ProjectInfo.Parameter.reviewCamGain1.ToString();
            this.tB_reviewCam_gain2.Text = ProjectInfo.Parameter.reviewCamGain2.ToString();
            this.tB_reviewCam_gain3.Text = ProjectInfo.Parameter.reviewCamGain3.ToString();

            this.tB_siMo_exposeTime.Text = ProjectInfo.Parameter.simoCamExposure.ToString();
            this.tB_siMo_Gamma.Text = ProjectInfo.Parameter.simoCamGamma.ToString();

            this.tB_scanCode_exposeTime.Text = ProjectInfo.Parameter.scanCodeCamExposure.ToString();
            this.tB_scanCode_Gamma.Text = ProjectInfo.Parameter.scanCodeCamGamma.ToString();

            this.tB_assembly_Gamma.Text = ProjectInfo.Parameter.downCamGamma.ToString();     //下相机组装Gamma
        }
        private void InitialCameralDeveice()
        {
            List<string> deviceInfo = new List<string>();
            if (ShareData.MvsCameral.CameralDeviceListAcq(out deviceInfo))   //初始化设备，可以重复初始化设备
            {
                for (int i = 0; i < deviceInfo.Count; i++)
                {
                    this.cbDeviceList.Items.Add(deviceInfo[i]);     //将检索出的相机罗列出来
                }
                if (deviceInfo.Count > 0)
                {
                    this.cbDeviceList.SelectedIndex = 0;
                }
            }
            else
                MessageBox.Show("相机初始化失败！");
        }
        void MvsCameral_captureImage(int cameralIndex, Bitmap image)
        {
            //this.pictureBox1.Image = image;  //显示图像
            this.Invoke(new Action(() =>
            {
                if (cameralIndex == this.cbDeviceList.SelectedIndex)
                {
                    takePicTime.Stop();
                    this.takePictureTime.Text = "取图时间：" + takePicTime.ElapsedMilliseconds + "ms";
                    this.lab_ImageInfo.Text = image.Width.ToString() + "*" + image.Height.ToString();     //显示宽高
                    this.pictureBox1.Image = image;                                                       //显示图像
                    //ShareData.statusInfoList[curStationIndex].imageFromCam = image;
                }
            }));
        }
        private void CloseExistCameral()
        {
            for (int i = 0; i < ShareData.cameralOpenList.Count; i++)
            {
                ShareData.MvsCameral.DestroyHKCam(ShareData.cameralOpenList[i]);   //关闭相机
            }
            ShareData.cameralOpenList.Clear();
        }
        private void btn_openDevice_Click(object sender, EventArgs e)
        {
            if (this.cbDeviceList.SelectedIndex > -1)
            {
                if (!ShareData.MvsCameral.OpenDevice(this.cbDeviceList.SelectedIndex))   //打开设备
                {
                    MessageBox.Show("打开相机失败");
                    return;
                }
                float exposeTime, gain, gamma;
                ShareData.MvsCameral.GetCameralParam(this.cbDeviceList.SelectedIndex, out exposeTime, out gain, out gamma);  //获得相机参数，曝光、增益、伽马
                this.tB_exposeTime.Text = exposeTime.ToString("f2");
                this.tB_gain.Text = gain.ToString("f2");
                this.tB_gamma.Text = gamma.ToString("f2");

                uint rectX, rectY, rectWidth, rectHeight;
                ShareData.MvsCameral.GetCameralROIPara(this.cbDeviceList.SelectedIndex, out rectX, out rectY, out rectWidth, out rectHeight);  //获取相机的ROI

                this.btn_CloseDevice.Enabled = true;
                this.gB_showSet.Enabled = true;
                //this.gB_triggerSet.Enabled = true;
                //this.btn_saveImage.Enabled = true;
                this.btn_StartCapture.Enabled = true;
                this.btn_softwareTriggerOnce.Enabled = false;
                this.btn_openDevice.Enabled = false;
                this.cB_triggerModel.SelectedIndex = 0;
                this.cB_triggerSource.SelectedIndex = 0;
                this.cbDeviceList.Enabled = false;
            }   
        }

        private void btn_StartCapture_Click(object sender, EventArgs e)
        {
            if (this.btn_StartCapture.Text == "开始采集")
            {
                if (ShareData.MvsCameral.StartHKCamGrab(this.cbDeviceList.SelectedIndex) != 0)//开始采集
                {
                    MessageBox.Show("开始采集失败！");
                    return;
                }
                else
                {
                    this.btn_softwareTriggerOnce.Enabled = true;
                }
                this.btn_StartCapture.Text = "停止采集";
            }
            else if (this.btn_StartCapture.Text == "停止采集")
            {
                if (ShareData.MvsCameral.StopHKCamGrab(this.cbDeviceList.SelectedIndex) != 0)    //停止采集
                {
                    MessageBox.Show("停止采集失败！");
                    return;
                }
                else
                {
                    this.btn_softwareTriggerOnce.Enabled = false;
                }
                this.btn_StartCapture.Text = "开始采集";
            } 
        }

        private void btn_CloseDevice_Click(object sender, EventArgs e)
        {
            if (this.cbDeviceList.SelectedIndex > -1)
            {
                if (ShareData.MvsCameral.DestroyHKCam(this.cbDeviceList.SelectedIndex) != 0)
                    MessageBox.Show("关闭设备失败！");
                else
                {
                    InitialControl();
                    this.btn_openDevice.Enabled = true;
                    this.cbDeviceList.Enabled = true;
                }
            }
        }
        private void InitialControl()
        {
            this.gB_showSet.Enabled = false;
            //this.gB_triggerSet.Enabled = false;
            //this.btn_saveImage.Enabled = false;
            this.btn_CloseDevice.Enabled = false;
            this.btn_StartCapture.Enabled = false;

        }
        private void btn_softwareTriggerOnce_Click(object sender, EventArgs e)
        {
            ShareData.MvsCameral.SoftTriggerOnce(this.cbDeviceList.SelectedIndex);   //软触发一次
            takePicTime.Reset();
            takePicTime.Start();
        }

        private void btn_quit_Click(object sender, EventArgs e)
        {
            if (this.btn_StartCapture.Enabled == false && this.btn_openDevice.Enabled == false)  //说明相机已经开始采集
            {
                if (ShareData.MvsCameral.StopHKCamGrab(this.cbDeviceList.SelectedIndex) != 0)    //停止采集
                    MessageBox.Show("停止采集失败！");
                else
                    this.btn_StartCapture.Enabled = true;
            }
            if (this.btn_openDevice.Enabled == false)     //说明已打开设备
            {
                if (ShareData.MvsCameral.DestroyHKCam(this.cbDeviceList.SelectedIndex) != 0)   //关闭设备
                    MessageBox.Show("关闭设备失败！");
                else
                    this.btn_openDevice.Enabled = true;
            }
            ShareData.MvsCameral.captureImage -= MvsCameral_captureImage;  //取消委托

            this.Close();
        }

        private void btn_setAsDownCam_Click(object sender, EventArgs e)
        {
            if (this.cbDeviceList.SelectedIndex > -1)
            {
                this.tB_downCamName.Text = this.cbDeviceList.SelectedItem.ToString();
                ProjectInfo.Parameter.downCamName = this.cbDeviceList.SelectedItem.ToString();
                ProjectInfo.Parameter.downCamIndex = this.cbDeviceList.SelectedIndex;    //相机索引
            }
            else
                MessageBox.Show("请先选择一相机！");
        }

        private void btn_setAsUpCam_Click(object sender, EventArgs e)
        {
            if (this.cbDeviceList.SelectedIndex > -1)
            {
                this.tB_upCamName.Text = this.cbDeviceList.SelectedItem.ToString();
                ProjectInfo.Parameter.upCamName = this.cbDeviceList.SelectedItem.ToString();
                ProjectInfo.Parameter.upCamIndex = this.cbDeviceList.SelectedIndex;    //相机索引
            }
            else
                MessageBox.Show("请先选择一相机！");
        }

        private void btn_saveCamPara_Click(object sender, EventArgs e)
        {
            try
            {
                ProjectInfo.Parameter.glueCamName = this.tB_glueCamName.Text;
                ProjectInfo.Parameter.guideCamName = this.tB_guideCamName.Text;
                ProjectInfo.Parameter.downCamName = this.tB_downCamName.Text;
                ProjectInfo.Parameter.upCamName = this.tB_upCamName.Text;
                ProjectInfo.Parameter.reviewCamName = this.tB_reviewCamName.Text;

                ProjectInfo.Parameter.glueCamExposure = float.Parse(this.tB_glueCam_expose.Text);
                ProjectInfo.Parameter.guideCamExposure = float.Parse(this.tB_guideCam_expose.Text);
                ProjectInfo.Parameter.downCamExposure = float.Parse(this.tB_downCam_expose.Text );
                ProjectInfo.Parameter.upCamExposure1 = float.Parse(this.tB_upCam_exposeTime1.Text);
                ProjectInfo.Parameter.upCamExposure2 = float.Parse(this.tB_upCam_exposeTime2.Text);
                ProjectInfo.Parameter.reviewCamExposure1 = float.Parse(this.tB_reviewCam_expose1.Text);
                ProjectInfo.Parameter.reviewCamExposure2 = float.Parse(this.tB_reviewCam_expose2.Text);
                ProjectInfo.Parameter.reviewCamExposure3 = float.Parse(this.tB_reviewCam_expose3.Text);

                ProjectInfo.Parameter.glueCamGain = float.Parse(this.tB_glueCam_gain.Text);
                ProjectInfo.Parameter.guideCamGain = float.Parse(this.tB_guideCam_gain.Text);
                ProjectInfo.Parameter.downCamGain = float.Parse(this.tB_downCam_gain.Text);
                ProjectInfo.Parameter.upCamGain1 = float.Parse(this.tB_upCam_gain1.Text);
                ProjectInfo.Parameter.upCamGain2 = float.Parse(this.tB_upCam_gain2.Text);
                ProjectInfo.Parameter.reviewCamGain1 = float.Parse(this.tB_reviewCam_gain1.Text);
                ProjectInfo.Parameter.reviewCamGain2 = float.Parse(this.tB_reviewCam_gain2.Text);
                ProjectInfo.Parameter.reviewCamGain3 = float.Parse(this.tB_reviewCam_gain3.Text);

                ProjectInfo.Parameter.simoCamExposure = float.Parse(this.tB_siMo_exposeTime.Text);    //撕膜工位曝光设置
                ProjectInfo.Parameter.simoCamGamma = float.Parse(this.tB_siMo_Gamma.Text);
                ProjectInfo.Parameter.scanCodeCamExposure = float.Parse(this.tB_scanCode_exposeTime.Text); 
                ProjectInfo.Parameter.scanCodeCamGamma = float.Parse(this.tB_scanCode_Gamma.Text );
                ProjectInfo.Parameter.downCamGamma = float.Parse(this.tB_assembly_Gamma.Text);       //下相机组装Gamma

                ProjectInfo.SaveParameter(ShareData.configPath); //保存配置文件   
                MessageBox.Show("参数保存成功！"); 
            }
            catch(Exception ex)
            {
                MessageBox.Show("保存参数失败！" + ex.Message);
            }
        }

        private void cbDeviceList_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void tB_exposeTime_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                float exposeTime = 0;
                if (float.TryParse(this.tB_exposeTime.Text, out exposeTime))
                {
                    if (ShareData.MvsCameral.SetHKCamExposure(this.cbDeviceList.SelectedIndex, exposeTime) == -1)
                        MessageBox.Show("设置曝光失败！");
                }
                else
                {
                    MessageBox.Show("曝光格式设置有误！");
                }
            }
        }

        private void tB_exposeTime_Leave(object sender, EventArgs e)
        {
            float exposeTime = 0;
            if (float.TryParse(this.tB_exposeTime.Text, out exposeTime))
            {
                if (ShareData.MvsCameral.SetHKCamExposure(this.cbDeviceList.SelectedIndex, exposeTime) == -1)
                    MessageBox.Show("设置曝光失败！");
            }
            else
            {
                MessageBox.Show("曝光格式设置有误！");
            }
        }

        private void tB_gamma_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                float gamma = 0;
                if (float.TryParse(this.tB_gamma.Text, out gamma))
                {
                    if (ShareData.MvsCameral.SetHKCamGamma(this.cbDeviceList.SelectedIndex, gamma) == -1)
                        MessageBox.Show("设置伽马值失败！");
                }
                else
                {
                    MessageBox.Show("伽马格式设置有误！");
                }
            }
        }

        private void tB_gamma_Leave(object sender, EventArgs e)
        {
            float gamma = 0;
            if (float.TryParse(this.tB_gamma.Text, out gamma))
            {
                if (ShareData.MvsCameral.SetHKCamGamma(this.cbDeviceList.SelectedIndex, gamma) == -1)
                    MessageBox.Show("设置伽马值失败！");
            }
            else
            {
                MessageBox.Show("伽马格式设置有误！");
            }
        }

        private void tB_gain_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                float gain = 0;
                if (float.TryParse(this.tB_gain.Text, out gain))
                {
                    if (ShareData.MvsCameral.SetHKCamGain(this.cbDeviceList.SelectedIndex, gain) == -1)
                        MessageBox.Show("设置增益失败！");
                }
                else
                {
                    MessageBox.Show("增益格式设置有误！");
                }
            }
        }

        private void tB_gain_Leave(object sender, EventArgs e)
        {
            float gain = 0;
            if (float.TryParse(this.tB_gain.Text, out gain))
            {
                if (ShareData.MvsCameral.SetHKCamGain(this.cbDeviceList.SelectedIndex, gain) == -1)
                    MessageBox.Show("设置增益失败！");
            }
            else
            {
                MessageBox.Show("增益格式设置有误！");
            }
        }

        private void cB_triggerModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.cB_triggerModel.SelectedIndex)
            {
                case 0:
                    ShareData.MvsCameral.SetTriggerGrab(this.cbDeviceList.SelectedIndex);   //设置为触发模式
                    this.cB_triggerSource.Enabled = true;
                    this.btn_softwareTriggerOnce.Enabled = true;
                    break;
                case 1:
                    ShareData.MvsCameral.SetContinueGrab(this.cbDeviceList.SelectedIndex);   //设置为连续采集模式
                    this.cB_triggerSource.Enabled = false;
                    this.btn_softwareTriggerOnce.Enabled = false;
                    break;
            }
        }

        private void cB_triggerSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.cB_triggerSource.SelectedIndex)
            {
                case 0:
                    ShareData.MvsCameral.SetSoftTrigger(this.cbDeviceList.SelectedIndex);   //设置为软触发模式
                    this.btn_softwareTriggerOnce.Enabled = true;
                    break;
                case 1:
                    ShareData.MvsCameral.SetHardTrigger(this.cbDeviceList.SelectedIndex, 0);   //设置为硬模式line_0
                    this.btn_softwareTriggerOnce.Enabled = false;
                    break;
                case 2:
                    ShareData.MvsCameral.SetHardTrigger(this.cbDeviceList.SelectedIndex, 1);   //设置为硬模式line_1
                    this.btn_softwareTriggerOnce.Enabled = false;
                    break;
            }
        }

        private void btn_setAsReviewCam_Click(object sender, EventArgs e)
        {
            if (this.cbDeviceList.SelectedIndex > -1)
            {
                this.tB_reviewCamName.Text = this.cbDeviceList.SelectedItem.ToString();
                ProjectInfo.Parameter.reviewCamName = this.cbDeviceList.SelectedItem.ToString();
                ProjectInfo.Parameter.reviewCamIndex = this.cbDeviceList.SelectedIndex;    //相机索引
            }
            else
                MessageBox.Show("请先选择一相机！");
        }

        private void btn_saveImage_Click(object sender, EventArgs e)
        {

        }

        private void btn_setAsGlueCam_Click(object sender, EventArgs e)
        {
            if (this.cbDeviceList.SelectedIndex > -1)
            {
                this.tB_glueCamName.Text = this.cbDeviceList.SelectedItem.ToString();
                ProjectInfo.Parameter.glueCamName = this.cbDeviceList.SelectedItem.ToString();
                ProjectInfo.Parameter.glueCamIndex = this.cbDeviceList.SelectedIndex;    //相机索引
            }
            else
                MessageBox.Show("请先选择一相机！");
        }

        private void btn_setAsGuideCam_Click(object sender, EventArgs e)
        {
            if (this.cbDeviceList.SelectedIndex > -1)
            {
                this.tB_guideCamName.Text = this.cbDeviceList.SelectedItem.ToString();
                ProjectInfo.Parameter.guideCamName = this.cbDeviceList.SelectedItem.ToString();
                ProjectInfo.Parameter.guideCamIndex = this.cbDeviceList.SelectedIndex;    //相机索引
            }
            else
                MessageBox.Show("请先选择一相机！");
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void btn_saveImage_Click_1(object sender, EventArgs e)
        {
            if(this.pictureBox1.Image != null)
            {
                this.pictureBox1.Image.Save(DateTime.Now.ToString() + "image.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }
    }
}
